import axios from './axios'
import qs from 'qs'
import { baseUrl } from '@/config/config'
import menu from '../mock/menuMock'
/**
 * 登录
 * @param {} params
 */
export const login = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/login',
      method: 'post',
      data: params
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error)
    })
  })
}
/**
 * 登出
 * @param {} params
 */
export const logout = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/api/sysuser/logout',
      method: 'get'
    }).then(res => {
      resolve(res.data)
    })
  })
}
/**
 * 刷新token
 * @param {refreshToken} params
 */
export const refreshToken = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/api/sysuser/refresh',
      method: 'get',
      headers: {
        'refresh_token': params.refreshToken
      }
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error)
    })
  })
}
/**
 * 修改密码
 * @param {} params
 */
export const chanPwd = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/api/password',
      method: 'put',
      data: params
    }).then(res => {
      var data = res.data
      // console.log(res)
      if (data.code == 1) {
        resolve({ message: '密码修改成功', success: true })
      } else if (data.code == 201) {
        resolve({ message: '密码修改失败，原密码错误', success: false })
      }
    })
  })
}
/**
 * 获取菜单列表
 * @param {} params
 */
export const getMenu = (params) => {
  return new Promise((resolve, reject) => {
    resolve({ data: menu[params], message: '获取成功', success: true })
  })
}
/**
 * 获取系统权限集合
 * @param {} params
 */
export const getAuthority = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/api/authority',
      method: 'get'
    }).then(res => {
      resolve(res.data)
    }).catch(e => {

    })
  })
}
/**
 * 获取系统权限集合生成菜单列表
 * @param {} params
 */
export const getMenuAuthority = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/api/authority',
      method: 'get'
    }).then(res => {
      var permissionList = res.data.data
      permissionList.forEach((item, index) => {
        permissionList[index].label = item.describe
        permissionList[index].permission = []
        permissionList[index].show = false
        permissionList[index].href = ''
        params.forEach((auth, i) => {
          if (item.code == auth.code) {
            permissionList[index].permission = auth.permissions.indexOf('edit') >= 0 ? ['view', 'add', 'delete', 'edit'] : auth.permissions.split(',')
            if (item.describe == '代理管理') {
              permissionList[index].href = '/index/home'
              permissionList[index].icon = 'fa fa-area-chart'
              permissionList[index].show = permissionList[index].permission[0] != ''
            }
            if (item.describe == '代理配置') {
              permissionList[index].href = '/sysConfig/index'
              permissionList[index].icon = 'fa fa-address-book'
              permissionList[index].show = permissionList[index].permission[0] != ''
            }
            if (item.describe == '伯乐奖') {
              permissionList[index].href = '/bole/index'
              permissionList[index].icon = 'fa fa-list-alt'
              permissionList[index].show = permissionList[index].permission[0] != ''
            }
            if (item.describe == '贡献奖') {
              permissionList[index].href = '/contribution/index'
              permissionList[index].icon = 'fa fa-tags'
              permissionList[index].show = permissionList[index].permission[0] != ''
            }
            // -------------------------------
            if (item.describe == '系统用户管理') {
              permissionList[index].href = '/system/index'
              permissionList[index].icon = 'fa fa-gears'
              permissionList[index].show = permissionList[index].permission[0] != ''
            }
            if (item.describe == '角色管理') {
              permissionList[index].href = '/system/role-index'
              permissionList[index].icon = 'fa fa-users'
              permissionList[index].show = permissionList[index].permission[0] != ''
            }
          }
        })
      })
      console.log('permissionList', permissionList)
      resolve({ data: permissionList, message: '获取成功', success: true })
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * @name: 获取短信验证码
 * @param {string} key 短信参数
 */
 export const getVerification = (key) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/verification/' + key,
      method: 'get'
    }).then(res => {
      resolve(res.data)
    })
  })
}

/**
 * @name: 校验短信验证码 n
 * @param {string} key 短信参数
 */
export const checkVerification = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/check/verification',
      method: 'get',
      params
    }).then(res => {
      resolve(res.data)
    })
  })
}

/**
 * @name:
 * @test: test font
 * @msg:
 * @param {type}
 * @return:
 */
export const editPassword = (data) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/api/sysuser/update/pwd',
      method: 'PUT',
      data
    }).then(res => {
      resolve(res.data)
    })
  })
}
